package com.gxy.learn.securityauth.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.cache.jcache.JCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Description(Ehcache配置)
 * author: Gao xueyong
 * Create at: 2022/1/12 下午11:51
 */
@Component
public class CacheConfig extends CachingConfigurerSupport {
    @Bean
    public JCacheManagerFactoryBean cacheManagerFactoryBean() throws IOException {
        JCacheManagerFactoryBean factoryBean = new JCacheManagerFactoryBean();
        // 配置全部写在ehcache.xml这个配置文件内~~~~
        factoryBean.setCacheManagerUri(new ClassPathResource("ehcache.xml").getURI());
        return factoryBean;
    }

    @Bean
    public CacheManager cacheManager(javax.cache.CacheManager cacheManager) {
        // 它必须要包装一个javax.cache.CacheManager，也就是Eh107CacheManager才行
        JCacheCacheManager cacheCacheManager = new JCacheCacheManager();
        // 方式一：使用`JCacheManagerFactoryBean` + xml配置文件的方式
        cacheCacheManager.setCacheManager(cacheManager);
        return cacheCacheManager;
    }
}
